package com.cmccgz.udacs_landlord_webapp.main.user.userinfo;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.baselibrary.image.GlideUtils;
import com.cmccgz.baselibrary.utils.BoxingUtils;
import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.baselibrary.view.photoview.PhotoView;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

import java.util.List;

/**
 * @author INX
 * 查看用户头像fragment
 */
public class UserHeaderShowFragment extends BaseFragment implements View.OnClickListener {
    private PhotoView pv_header;
    public static final String KEY_HEADER_URL="key_header_url";
    private UserInfoActivity parentActivity;
    private String headerUrl;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static UserHeaderShowFragment instance(){
        return new UserHeaderShowFragment();
    }
    @Override
    protected int setLayoutId() {
        return R.layout.fragment_user_header_show;
    }

    @Override
    protected void initView() {
        pv_header = ((PhotoView) findViewById(R.id.pv_header));
        ViewHelper viewHelper = ViewHelper.instance(this);
        if (viewHelper!=null) {
            viewHelper.setBackTextVisable(true);
            viewHelper.setTextRightVisable(true);
            viewHelper.setRightText(R.string.right_text_header_show);
            viewHelper.setTitle(R.string.title_header_show);
        }
        parentActivity = ((UserInfoActivity) mContext);
        iniDatas();
    }

    private void iniDatas() {
        Bundle arguments = getArguments();
        if (isSaveInstance) {
            Bundle savedInstanceState = getSavedInstanceState();
            headerUrl = savedInstanceState!=null?savedInstanceState.getString("headerUrl",""):"";
        } else if (arguments != null) {
            headerUrl = arguments.getString(KEY_HEADER_URL);
            if (StringUtils.isEmpty(headerUrl)) {
                showToast(R.string.tips_no_header_url);
            }
        }
        if (!StringUtils.isEmpty(headerUrl)) {
            GlideUtils.loadImage(mContext,headerUrl,pv_header);
            parentActivity.updateUserHeaderByChange(headerUrl);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_right:
                BoxingUtils.selectSingleImage(mContext,true);
               /* if (selectDialog==null) {
                    selectDialog = takePhotoUtils.getSelectDialog(mContext,1,true);
                }
                if (!selectDialog.isShowing()) {
                    selectDialog.show();
                }*/
                break;
            case R.id.tv_back:
                mContext.onBackPressed();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    public void setHeader(String path) {
        headerUrl=path;
        if (!StringUtils.isEmpty(headerUrl)) {
            GlideUtils.loadImage(mContext,headerUrl,pv_header);
            parentActivity.updateUserHeaderByChange(headerUrl);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<String> urlResult = BoxingUtils.getUrlResult(requestCode, resultCode, data, mContext, 1024 * 1024);
        if (urlResult!=null&&urlResult.size()>0) {
            setHeader(urlResult.get(0));
        }
//        takePhotoUtils.getTakePhoto().onActivityResult(requestCode,resultCode,data);
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("headerUrl",headerUrl);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroyView() {
        isSaveInstance=false;
        super.onDestroyView();
    }
}
